//
//  SecondViewController.m
//  Socrates
//
//  Created by shy on 15/3/2.
//  Copyright (c) 2015年 shy. All rights reserved.
//

#import "MessageViewController.h"

#import "InboxViewController.h"
#import "NotificationViewController.h"
#import "ContactsViewController.h"


static const CGFloat kSegmentedItemWidth = 72.f;

@interface MessageViewController ()

@property(strong, nonatomic) UISegmentedControl *segmentControl;
@property(weak, nonatomic) UIViewController *currentViewController;
@property(strong, nonatomic) NSArray *viewControllers;
@property (strong, nonatomic) UIBarButtonItem *contactsRightBarBtn;

@end

@implementation MessageViewController

- (void)dealloc {
    NSLog(@"%@ dealloc", self.class);
//    [[NSNotificationCenter defaultCenter] removeObserver:self
//                                                    name:ConversationUnreadCount
//                                                  object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(conversationUnreadCount:)
//                                                 name:ConversationUnreadCount
//                                               object:nil];
    
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[
                                                                      NSLocalizedString(@"私信", nil),
                                                                      NSLocalizedString(@"通知", nil)
                                                                      ]];
    for (int i = 0; i < self.segmentControl.numberOfSegments; i++) {
        [self.segmentControl setWidth:kSegmentedItemWidth forSegmentAtIndex:i];
    }
    self.segmentControl.selectedSegmentIndex = 0;
    [self.segmentControl addTarget:self
                            action:@selector(segmentChanged:)
                  forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.segmentControl;
    self.navigationItem.rightBarButtonItem = [self makeBarBtn];
}

- (UIBarButtonItem *)makeBarBtn {
    UIButton *settings = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 28, 25)];
    [settings setBackgroundImage:[UIImage imageNamed:@"add_friend"] forState:UIControlStateNormal];
    [settings addTarget:self action:@selector(clickRightBarButton:) forControlEvents:UIControlEventTouchUpInside];
    self.contactsRightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:settings];
    
    return self.contactsRightBarBtn;
}

- (void)clickRightBarButton:(id)sender {
    ContactsViewController *vc = [[ContactsViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self segmentChanged:self.segmentControl];
//    [self updateUnreadChatCount:[ConversationCount instance].unreadCount];
//    NSUInteger unreadNotificationsCount = [RSTNotification loadUnreadNotifications].count;
//    if ([UserPreferences unreadNotificationCount] > 0) {
//        // unread count in prefs doesn't merged by story, so only show an indicator
//        [self showUnreadNotificationIndicator];
//        [UserPreferences saveUnreadNotificationCount:0];  // should clear after loading data from server
//    } else {
//        [self updateUnreadNotificationsCount:unreadNotificationsCount];
//    }
}

- (void)segmentChanged:(UISegmentedControl *)sender {
    [self.currentViewController.view removeFromSuperview];
    [self.currentViewController removeFromParentViewController];
    
    UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
    vc.edgesForExtendedLayout = UIRectEdgeNone;
    CGRect frame = [UIScreen mainScreen].bounds;
    frame.size.height = CGRectGetHeight([UIScreen mainScreen].bounds) - 64;
    vc.view.frame = frame;
    [self addChildViewController:vc];
    [self.view addSubview:vc.view];
    [vc didMoveToParentViewController:self];
    
    self.currentViewController = vc;
    self.navigationItem.title = vc.title;
}

- (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
    if (!self.viewControllers) {
        self.viewControllers = @[
                                 [[InboxViewController alloc] init],
                                 [[NotificationViewController alloc] init],
                                ];
    }
    return self.viewControllers[index];
}

- (void)conversationUnreadCount:(NSNotification*)notification {
    NSUInteger unreadCount = [[[notification userInfo] objectForKey:@"unread"] unsignedIntValue];
    [self updateUnreadChatCount:unreadCount];
}

- (void)updateUnreadChatCount:(NSUInteger)count {
    NSString *title;
    if (count > 0) {
        title = [NSString stringWithFormat:@"%@ (%lu)", NSLocalizedString(@"$$$私信", nil), (unsigned long)count];
    } else {
        title = NSLocalizedString(@"通知", nil);
    }
    [self.segmentControl setTitle:title forSegmentAtIndex:0];
}

- (void)updateUnreadNotificationsCount:(NSUInteger)count {
    NSString *title;
    if (count > 0) {
        title = [NSString stringWithFormat:@"%@ (%lu)", NSLocalizedString(@"通知", nil), (unsigned long)count];
    } else {
        title = NSLocalizedString(@"通知", nil);
    }
    [self.segmentControl setTitle:title forSegmentAtIndex:1];
}

- (void)showUnreadNotificationIndicator {
    [self.segmentControl setTitle:[NSString stringWithFormat:@"%@ ●", NSLocalizedString(@"通知", nil)] forSegmentAtIndex:1];
}


@end
